package com.code;


/**
 * 获取 股票最大收益
 * Created by lucky on 2018/9/27.
 */
public class Code6 {
    public static void main(String[] args) {
        int[] prices = new int[]{7,1,5,3,6,4};
        int i = maxProfit2(prices);
        System.out.println(i);
    }

    public static int maxProfit(int[] prices) {
        int result = 0;
        int m = 0;
        for (int i = 0 ; i < prices.length ;i ++){
            int temp = prices[i];
            for (int j = i + 1 ; j < prices.length ;j++){
                if(temp < prices[j]){
                    temp = prices[j];
                    m = j;
                }else{
                    break;
                }
            }
            if(temp != prices[i]){
                result += temp - prices[i];
                i = m;
            }
        }
        return result;
    }

    public static int maxProfit2(int[] prices) {
        int result = 0;
        for (int i = 0 ; i < prices.length ;i ++){
            int temp = prices[i];
            for (int j = i + 1 ; j < prices.length ;j++){
                if(temp < prices[j]){
                    temp = prices[j];
                }
            }
            if(temp != prices[i]){
                int m = temp - prices[i];
                if(m > result){
                    result = m;
                }
            }
        }
        return result;
    }
}
